package com.cms.manage.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cms.manage.common.ConmonConfig;
import com.cms.manage.common.SysCode;
import com.cms.manage.dao.CmsClassfyMapper;
import com.cms.manage.dao.CmsStoreListMapper;
import com.cms.manage.dao.CmsStoreLogMapper;
import com.cms.manage.dao.CmsStoreMapper;
import com.cms.manage.dao.CmsUserMapper;
import com.cms.manage.model.CmsClassfy;
import com.cms.manage.model.CmsStore;
import com.cms.manage.model.CmsStoreList;
import com.cms.manage.model.CmsStoreLog;
import com.cms.manage.model.CmsUser;
import com.cms.manage.service.StoreListService;
import com.cms.manage.util.MapUtil;
import com.cms.manage.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * Created by sgy on 2017/4/7.
 */
@Service
public class StoreListServiceImpl extends BaseService<CmsStoreList> implements StoreListService {

	
    @Autowired
    private  CmsClassfyMapper cmsClassfyMapper;
    @Autowired
    private  CmsStoreListMapper cmsStoreListMapper;
    @Autowired
    private  CmsStoreMapper cmsStoreMapper;
    @Autowired
    private  CmsStoreLogMapper cmsStoreLogMapper;
    
    @Autowired 
	private CmsUserMapper cmsUserMapper;
   
	    /**
	     * 方法功能: 保存提货单
		 * 参数 bank
		 * 非空： bankName: "供应商", goodName: "品名",  number: 12 数量，area: "212.1"面积 userId:用户id userName:用户名字
		 * rank: "等级说明"，date:'sda',goodSize: "规格", classfy: "1"分类,
	     * Created by sgy on 2020/6/8.
	     */
		@Override
		public Map<String, Object> saveList(Map<String, String> data) {
			Map<String, Object> result = ConmonConfig.getResultMap();	
			StringBuffer message = new StringBuffer(); 
			CmsStoreList cmsStoreList = new CmsStoreList();
			//校验非空
			if(StringUtils.isBlank(data.get("bankName"))){
				result.put("code",SysCode.error);
				message.append("供应商不能为空! ");
			}
			if(StringUtils.isBlank(data.get("goodName"))){
				result.put("code",SysCode.error);
				message.append("品名不能为空! ");
			}
			if(MapUtil.getInt(data, "number")==null){
				result.put("code",SysCode.error);
				message.append("数量不能为空! ");
			}
			if(StringUtils.isBlank(data.get("area"))){
				result.put("code",SysCode.error);
				message.append("面积不能为空! ");
			}
			if(StringUtils.isBlank(data.get("userTotalId"))){
				result.put("code",SysCode.error);
				message.append("提交人不能为空! ");
			}
			//获取最新的用户对象
			CmsUser cmsUser = cmsUserMapper.selectByPrimaryKey(MapUtil.getInt(data, "userTotalId"));
			//判断分类字典是否正常
			if(MapUtil.getShort(data, "classfy")!=null){
				CmsClassfy cmsClassfy = cmsClassfyMapper.selectByPrimaryKey(MapUtil.getInt(data, "classfy"));
				if(cmsClassfy==null){
					result.put("code",SysCode.error);
					message.append("当前提交的分类已被删除! ");
				}else{
					cmsStoreList.setClassfy(MapUtil.getShort(data, "classfy"));
					cmsStoreList.setClassfyname(cmsClassfy.getName());
				}
			}				
			if(!(result.get("code")==SysCode.error)){//必录字段都存在
				cmsStoreList.setBankName(MapUtil.getString(data, "bankName"));
				cmsStoreList.setGoodName(MapUtil.getString(data, "goodName"));
				cmsStoreList.setGoodSize(MapUtil.getString(data, "goodSize"));			
				cmsStoreList.setNumber(MapUtil.getInt(data, "number"));
				cmsStoreList.setArea(MapUtil.getString(data, "area"));
				cmsStoreList.setRank(MapUtil.getString(data, "rank"));
				cmsStoreList.setDate(MapUtil.getDate(data,"date","yyyy-MM-dd"));
				cmsStoreList.setCreateTime(new Date());
				cmsStoreList.setUpdateTime(new Date());
				cmsStoreList.setUserId(cmsUser.getId());
				cmsStoreList.setUserName(cmsUser.getName());	
				if (StringUtils.isNoneBlank(MapUtil.getString(data,"id"))) {
					int id = MapUtil.getInt(data,"id");
					//查询寻原始数据是否存在
				    CmsStoreList res = cmsStoreListMapper.selectByPrimaryKey(id);
					//获取到原始数量,如果库存没变更，则不插入库存日志
					if(res!=null){
						boolean flag = true;
						int number = res.getNumber();
						int editNumber = MapUtil.getInt(data, "number");
						if(res.getNumber()==MapUtil.getInt(data, "number")){
							flag=false;						
						}	
						cmsStoreList.setId(id);
						int update = cmsStoreListMapper.updateByPrimaryKey(cmsStoreList);
						if(update==0){
							result.put("code",SysCode.error);
							message.append("更新失败,网络异常 ");
						}else{//插入库存变更日志
							if(flag){
								CmsStoreLog cmsStoreLog  = new CmsStoreLog();
								cmsStoreLog.setGoodName(MapUtil.getString(data, "goodName"));
								cmsStoreLog.setCreateTime(new Date());
								cmsStoreLog.setNumber(number);
								cmsStoreLog.setEditNumber(editNumber);
								cmsStoreLog.setOperate(new Short("5"));//编辑
								cmsStoreLog.setRelateId(cmsStoreList.getId());//入库单ID
								cmsStoreLog.setUserId(cmsUser.getId());
								cmsStoreLog.setUserName(cmsUser.getName());
								int logcont = cmsStoreLogMapper.insert(cmsStoreLog);
								if(logcont==0){
									//回滚数据				
									result.put("code",SysCode.error);
									message.append("库存编辑日志插入异常 ：提货单号--"+cmsStoreList.getId());
								}
							}
						}						
					}		
				}else{
					int insert = cmsStoreListMapper.insert(cmsStoreList);
					if(insert==0){
						result.put("code",SysCode.error);
						message.append("提交失败,网络异常 ");
					}
					 //插入流程日志
					//入库单操作 
					CmsStoreLog cmsStoreLog  = new CmsStoreLog();
					cmsStoreLog.setGoodName(MapUtil.getString(data, "goodName"));
					cmsStoreLog.setCreateTime(new Date());
					cmsStoreLog.setNumber(0);
					cmsStoreLog.setEditNumber(MapUtil.getInt(data, "number"));
					cmsStoreLog.setOperate(new Short("4"));//新增
					cmsStoreLog.setRelateId(cmsStoreList.getId());//入库单ID
					cmsStoreLog.setUserId(cmsUser.getId());
					cmsStoreLog.setUserName(cmsUser.getName());
					int logcont = cmsStoreLogMapper.insert(cmsStoreLog);
					if(logcont==0){
						//回滚数据				
						result.put("code",SysCode.error);
						message.append("日志插入异常 ：提货单号--"+cmsStoreList.getId());
					}
				}							   
			}		
			result.put("message", message.toString());			
			return result;
		}
		 /**
	     * 方法功能: 获取入库单列表
		 * 参数 classfy =分类  name==供应商或原料名字
	     * Created by sgy on 2020/6/8.
	     */
		@Override
		public Map<String, Object> searchStoreList(Map<String, String> data) {
			Map<String, Object> result = ConmonConfig.getResultMap();	
			StringBuffer message = new StringBuffer(); 
			//查询所有入库单
			    Example example = new Example(CmsStoreList.class);
				example.setOrderByClause("id desc");
				Criteria criteria=example.createCriteria();
				 Map<String,Object> param = new HashMap<>();
				 if(StringUtils.isNotBlank(MapUtil.getString(data, "classfy"))){//分类筛选
					 param.put("classfy", MapUtil.getString(data, "classfy"));
					 criteria.andEqualTo(param);
				 }					
				 if(StringUtils.isNotBlank(MapUtil.getString(data, "name"))){//名字筛选
					 Criteria criteria2=example.createCriteria();
					 criteria2.orLike("bankName", "%" + MapUtil.getString(data, "name") + "%");
				     criteria2.orLike("goodName", "%" + MapUtil.getString(data, "name") + "%");
				     example.and(criteria2);
				 }		
				PageHelper.startPage(MapUtil.getInt(data, "pageNum"), MapUtil.getInt(data, "pageSize"));
				List<CmsStoreList>  rs =cmsStoreListMapper.selectByExample(example);
			    //返回分页信息
			    PageInfo<CmsStoreList> pageInfo = new PageInfo<>(rs);
			    result.put("data", pageInfo);			
			    return result;
		}



	    



    

	

	
	
	
   
    
  
}